package com.hwapitest.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.type.TypeReference;

public class ReadDataFromYaml {
    public static List<Person> getData(String path) {
        ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
        try {
            List list = objectMapper.readValue(ReadDataFromYaml.class.getResourceAsStream(path), List.class);

            // 转成指定类型的list集合
            List<Person> list_new = objectMapper.convertValue(list, new TypeReference<List<Person>>() {});
            return list_new;

        } catch (Exception e) {
            System.out.println("异常：" + e);
        }
        return null;
    }

    public static void main(String[] args) {
        List<Person> plist = new ArrayList<Person>();
        List<Person> plist1 = getData("/caseData/person2.yaml");

        if ( plist1!=null){
            plist.addAll(plist1);
        }
        List<Person> plist2 = getData("/caseData/person3.yaml");
        if ( plist2!=null){
            plist.addAll(plist2);
            for (Person person : plist2) {
                System.out.println(person.getId());
                System.out.println(person.getName());
                System.out.println(person.getAge());
            }
        }
        System.out.println(plist);
    }
}